﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Xml;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate;
using FolderData;
using FarseerGames.FarseerPhysics.Collisions;

namespace Folder
{
    public class FolderGame : Microsoft.Xna.Framework.Game
    {
        #region Fields

        public GraphicsDeviceManager Graphics;
        ScreenManager screenManager;

        Song bgm_start, bgm_continue;

        #endregion

        #region Initialize and Load Contents

        public FolderGame()
        {
            Graphics = new GraphicsDeviceManager(this);
            Graphics.PreferredBackBufferWidth = Camera.SCREEN_WIDTH;
            Graphics.PreferredBackBufferHeight = Camera.SCREEN_HEIGHT;
            Content.RootDirectory = "Content";

            // Create the screen manager component.
            screenManager = new ScreenManager(this);

            Session.LoadSession(screenManager);

            Components.Add(screenManager);
            //Components.Add(new GamerServicesComponent(this));

            Session.SaveSession();
        }

        protected override void Initialize()
        {
            InputManager.Initialize();

            Graphics.GraphicsDevice.RenderState.AlphaBlendOperation = BlendFunction.Min;
            Graphics.GraphicsDevice.RenderState.SourceBlend = Blend.BothSourceAlpha;
            Graphics.GraphicsDevice.RenderState.DestinationBlend = Blend.One;
            Graphics.GraphicsDevice.RenderState.AlphaBlendEnable = true;

            Graphics.ApplyChanges();


            base.Initialize();
        }



        protected override void LoadContent()
        {
            RenderingHelper.Initialize(screenManager);
            Fonts.LoadContent(Content);

            VertexDeclaration quadVertexDecl = new VertexDeclaration(Graphics.GraphicsDevice,
                VertexPositionNormalTexture.VertexElements);
            GraphicsDevice.VertexDeclaration = quadVertexDecl;

            ResourceManager.Initialize(Content);
            loadBaseMap();

            bgm_start = Content.Load<Song>("BGM/bgm0_start");
            bgm_continue = Content.Load<Song>("BGM/bgm0_continue");

            MediaPlayer.Play(bgm_start);
            MediaPlayer.IsRepeating = false;
            MediaPlayer.MediaStateChanged += new EventHandler(MediaPlayer_MediaStateChanged);
            base.LoadContent();
        }

        void MediaPlayer_MediaStateChanged(object sender, EventArgs e)
        {
            if (MediaPlayer.State == MediaState.Stopped)
            {
                MediaPlayer.Play(bgm_continue);
                MediaPlayer.IsRepeating = true;
            }
        }

        protected void loadBaseMap()
        {

#if MAPEDITOR
            screenManager.AddScreen(new MainMenuScreen());
            screenManager.AddScreen(new GameplayScreen("map_new"));
#else
            screenManager.AddScreen(new BackgroundScreen());
            screenManager.AddScreen(new MainMenuScreen());
#endif
        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        #endregion

        #region Update

        protected override void Update(GameTime gameTime)
        {

            InputManager.Update();
            base.Update(gameTime);
        }

        #endregion

        #region Draw

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
        }


        #endregion
    }
}
